
#include "tools/socket.h"
#include "logging.h"
#include <stdlib.h>
#include <stdio.h>
#include "UfBuffer.h"
#include "UftClient.h"
#include "UftServer.h"


// 测试把200M数据全扔到网络上的速度
void test1()
{

    tools::CSocket  sock(tools::CSocket::eSocketType_UDP);
    sock.Create();
    sock.Bind(10234);

    int i;
    for (i = 0; i < 1024 * 200; i++) {
        char    buffer[1024];
        int ret = sock.SendTo("10.0.0.253", 10234, buffer, 1024);
        if (ret != 1024) {
            printf("ret = %d\n", ret);
        }
    }
}


// 多线程扔200M数据到网络上。
static tools::CSocket  sock2(tools::CSocket::eSocketType_UDP);
static void* _proc(void* arg)
{
    int i;
    for (i = 0; i < 1024 * 100; i++) {
        char    buffer[1024];
        int ret = sock2.SendTo("10.0.0.253", 10234, buffer, 1024);
        if (ret != 1024) {
            printf("ret = %d\n", ret);
        }
    }
    return NULL;
}

void test2()
{
    sock2.Create();
    sock2.Bind(10234);
    pthread_t   thread;
    pthread_create(&thread, NULL, _proc, NULL);

    int i;
    for (i = 0; i < 1024 * 100; i++) {
        char    buffer[1024];
        int ret = sock2.SendTo("10.0.0.253", 10234, buffer, 1024);
        if (ret != 1024) {
            printf("ret = %d\n", ret);
        }
    }

    pthread_join(thread, NULL);
}

void test_ufbuffer()
{
    const char a[] = {'\x55', '\x32', '\x33', '\x3', '\x4', '\x35', '\x0'};
    printf("%s\n", a);
    UfBuffer    buf(a, 7);
    RUN_HERE() << buf;
}

static void * _client_proc(void* arg)
{
    RUN_HERE();

    // logging::setThreadLogging(false);

    UftClient c;
    unlink("aaa.tmp");
    int ret = c.requestFile("127.0.0.1", 10234, "/home/kazuki/projects/Trunk.rar", "aaa.tmp");
    // int ret = c.requestFile("127.0.0.1", 10234, "logging.cpp", "aaa.tmp");
    RUN_HERE() << ret;

    ILOG() << "client end.";
    exit(0);
    return NULL;
}

int main(int argc, char* argv[])
{
    if (argc == 1) {
        UftServer s;

        pthread_t   thread;
        pthread_create(&thread, NULL, _client_proc, NULL);


        s.start(10234);
        pthread_join(thread, NULL);
    } else {
        if (argv[1][0] == 's') {
            UftServer s;
            s.start(10234);
        } else if (argv[1][0] == 'c') {
            _client_proc(NULL);
        }
    }

    // ScopeTrace(ReadAndThrow);
    // test2();
    return 0;
}









